package com.sudo.service.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.dao.admin.entity.SysUserBehavior;
import com.sudo.dao.admin.mapper.SysUserBehaviorMapper;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.admin.service.SysUserBehaviorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysUserBehaviorServiceImpl extends AdminBaseService implements SysUserBehaviorService {

    @Resource
    private SysUserBehaviorMapper sysUserBehaviorMapper;

    /**
     * 向数据库中增加用户行为日志
     * @param sysUserBehavior 日志对象
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void addSysUserBehavior(SysUserBehavior sysUserBehavior) {
        sysUserBehaviorMapper.insert(sysUserBehavior);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public PagedGridResult queryUserBehaviorByPage(Integer page, Integer pageSize, String username , String startDate , String endDate) {
        PageHelper.startPage(page, pageSize);
        Example example = new Example(SysUserBehavior.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(username)) {
            criteria.andEqualTo("username",username);
        }
        if (StringUtils.isNotBlank(startDate)) {
            criteria.andGreaterThanOrEqualTo("operTime",startDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            criteria.andLessThanOrEqualTo("operTime",endDate);
        }
        List<SysUserBehavior> sysUserBehaviorList = sysUserBehaviorMapper.selectByExample(example);
        return setterPagedGrid(sysUserBehaviorList, page);
    }
}
